using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;


namespace ASER.DataAccess.BaseDataAccess
{
    public static class FillDataFromReader<T> where T : new()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    {
        public static List<T> FillDataListGeneric(System.Data.IDataReader reader)
        {
            List<T> DataList = new List<T>();
            while (reader.Read())
            {
                T RowInstance = Activator.CreateInstance<T>();
                foreach (PropertyInfo property in typeof(T).GetProperties())
                {
                    object[] objs = property.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    string fieldName = ((DescriptionAttribute)objs[0]).Description;
                    try
                    {
                        int index = reader.GetOrdinal(fieldName);
                        object value = null;
                        if (property.PropertyType == typeof(System.Boolean))
                        {
                            value = reader[fieldName].ToString() == "" ? false : Convert.ToBoolean(reader[fieldName]);
                        }
                        else
                        {
                            value = reader[fieldName];
                        }
                        property.SetValue(RowInstance, ChangeType(value, property.PropertyType), null);
                    }
                    catch
                    {
                        continue;
                    }
                }

                DataList.Add(RowInstance);
            }
            return DataList;
        }

        public static T FillEntities(System.Data.IDataReader reader)
        {
            T RowInstance = Activator.CreateInstance<T>();
            if (reader.Read())
            {
                foreach (PropertyInfo property in typeof(T).GetProperties())
                {
                    object[] objs = property.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    string fieldName = ((DescriptionAttribute)objs[0]).Description;
                    try
                    {
                        int index = reader.GetOrdinal(fieldName);

                        object value = null;
                        if (property.PropertyType == typeof(System.Boolean))
                        {
                            value = reader[fieldName].ToString() == "" ? false : Convert.ToBoolean(reader[fieldName]);
                        }
                        else
                        {
                            value = reader[fieldName].ToString();
                        }
                        property.SetValue(RowInstance, ChangeType(value, property.PropertyType), null);
                    }
                    catch
                    {
                        continue;
                    }
                }

            }
            return RowInstance;
        }

        private static object ChangeType(object value, Type type)
        {
            if (value.ToString() == "" || value.ToString() == string.Empty || value == null)
            {
                if (type == typeof(decimal) || type == typeof(double) || type == typeof(int))
                {
                    return Convert.ChangeType(0, type);
                }
                else
                {
                    return Convert.ChangeType(value, type);
                }
            }
            else
                return Convert.ChangeType(value, type);
        }
    }
}

